package com.wn.class30.cotroller.houtai;

import com.wn.class30.entity.AdminUser;
import com.wn.class30.service.BackUserService;
import com.wn.class30.tools.Page;
import com.wn.class30.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class BackUserController {
    @Resource
    private BackUserService backUserService;

    @RequiresPermissions("/houtai/page/user/list")
    @RequestMapping("/houtai/page/user/list")
    public Result backUserList() throws Exception {
        return new Result("200","success",null,null);
    }
    @RequestMapping("/houtai/user/getUsers")
    public Result getUsersByPage(Integer page,Integer limit,String username, String tel, String lvId, String startDate,String endDate) throws Exception {
        Map<String,String> map = new HashMap<>();
        map.put("page",String.valueOf(page));
        map.put("limit",String.valueOf(limit) );
        map.put("tel", tel);
        map.put("username", username);
        map.put("lvId", lvId);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        Page pager = new Page();
        pager.setCurrentPage(page);
        pager.setPageSize(limit);
        int count = backUserService.selectCount(map);
        pager.setTotalSize(count);
        pager.setTotalPage(count%limit==0?count/limit:count/limit+1);
        return new Result("0","success",pager, (List) backUserService.select(map));

    }
    @RequestMapping("/")
    public void toLoginPage(HttpServletResponse response) throws IOException {
        //获取subject对象，该对象代表一个具体的客户端
        Subject subject = SecurityUtils.getSubject();
        //判断该客户端是否已经登陆或者使用了记住我功能
        if(!subject.isAuthenticated() && !subject.isRemembered()){
            //如果没有登陆并且没有使用rememberMe，则进行登录验证,并把token传入进去，realm会校验用户名和密码
            response.sendRedirect("/houtai/page/adminuser/login.html");
        }else{
            response.sendRedirect("/houtai/page/adminindex.html");
        }

    }
//后台首页查询数量
    @RequestMapping("/houtai/page/selectUserCount")
    public Result selectUserCount() throws Exception {
        return new Result("200","success",backUserService.selectUserCount(),null);
    }
    @RequestMapping("/houtai/page/selectProCount")
    public Result selectProCount() throws Exception {
        Subject subject = SecurityUtils.getSubject();
        AdminUser adminUser = (AdminUser)subject.getPrincipal();
        Integer businessId = adminUser.getBusinessId();
        if(businessId==0){
            return new Result("200","success",backUserService.selectProCount(),null);
        }else{
            return new Result("200","success",backUserService.selectProCountByBusinessId(businessId),null);
        }

    }
    @RequestMapping("/houtai/page/selectBusiCount")
    public Result selectBusiCount() throws Exception {
        return new Result("200","success",backUserService.selectBusiCount(),null);
    }
    @RequestMapping("/houtai/page/selectOrderCount")
    public Result selectOrderCount() throws Exception {
        Subject subject = SecurityUtils.getSubject();
        AdminUser adminUser = (AdminUser)subject.getPrincipal();
        Integer businessId = adminUser.getBusinessId();
        if(businessId==0) {
            return new Result("200", "success", backUserService.selectOrderCount(), null);
        }else{
            return new Result("200","success",backUserService.selectOrderCountByBusinessId(businessId),null);
        }
    }
}
